home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / CmdPopup / CMessPopup.cp < prev    next >
Encoding:
Text File  |  1997-07-21  |  4.2 KB  |  145 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. // CMessPopup.cp
  3. //
  4. // ©1997 Maxym Runov. All rights reserved.
  5. //
  6. // You are free to use this code in any project, 
  7. // but the copyright remains with me.
  8. // Any questions and notes are welcome.
  9. //
  10. // E-mail: max@sunbay.crimea.ua
  11. // ===========================================================================
  12. // Version 1.0
  13.  
  14. #include <UTextTraits.h>
  15. #include <UDrawingState.h>
  16. #include "CMessPopup.h"
  17.  
  18. // Internal definition
  19. #define kMarkSimbol            '•'
  20.  
  21.  
  22. // ---------------------------------------------------------------------------
  23. // Constructor
  24. // ---------------------------------------------------------------------------
  25.  
  26. CMessPopup::CMessPopup(LStream *inStream) : LStdPopupMenu(inStream) {
  27.     MenuHandle        theMenuH;
  28.  
  29.     mPopupMenuResID = 0;
  30.     mCommandNums    = 0;
  31.     mNumCommands    = 0;
  32.     theMenuH        = GetMacMenuH();
  33.     if(theMenuH) mPopupMenuResID = (*theMenuH)->menuID;
  34.     ReadCommandNumbers();
  35. }
  36.  
  37.  
  38. // ---------------------------------------------------------------------------
  39. // Destructor
  40. // ---------------------------------------------------------------------------
  41.  
  42. CMessPopup::~CMessPopup() {
  43.     // Free command number table
  44.     if(mCommandNums) ::DisposeHandle((Handle)mCommandNums);
  45. }
  46.  
  47.  
  48. #pragma mark -
  49.  
  50.  
  51. //-------------------------------------------------------------------------------------
  52. // SetValue
  53. //-------------------------------------------------------------------------------------
  54.  
  55. void    CMessPopup::SetValue(Int32 inValue) {
  56.     if(GetValue() != inValue) {
  57.         SetupCurrentMenuItem(inValue);
  58.         if(inValue < mMinValue) inValue = mMinValue;
  59.         else{
  60.             if(inValue > mMaxValue) inValue = mMaxValue;
  61.         }
  62.         mValue = inValue;
  63.         BroadcastValueMessage();
  64.         Draw(nil);
  65.     }
  66. }
  67.  
  68.  
  69. //-------------------------------------------------------------------------------------
  70. // SetupCurrentMenuItem
  71. //-------------------------------------------------------------------------------------
  72.  
  73. void    CMessPopup::SetupCurrentMenuItem(Int16 inCurrentItem) {
  74.     MenuHandle        theMenuH = GetMacMenuH();
  75.     if(theMenuH) {
  76.         if(GetValue() != inCurrentItem) {
  77.             Int16    oldItem = GetValue();            
  78.             // Remove the current mark
  79.             ::SetItemMark(theMenuH, oldItem, 0);
  80.         }
  81.         // Always make sure item is marked
  82.         ::SetItemMark(theMenuH, inCurrentItem, kMarkSimbol);
  83.     }
  84. }
  85.  
  86.  
  87. #pragma mark -
  88.  
  89.  
  90. // ---------------------------------------------------------------------------
  91. // ReadCommandNumbers
  92. // ---------------------------------------------------------------------------
  93.  
  94. void    CMessPopup::BroadcastValueMessage() {
  95.     Int32    theValue = mValue;
  96.     BroadcastMessage(CommandFromIndex(theValue), (void *)&theValue);
  97. }
  98.  
  99.  
  100. // ---------------------------------------------------------------------------
  101. // ReadCommandNumbers
  102. // ---------------------------------------------------------------------------
  103. //    Get command numbers from the 'Mcmd' resource
  104. //
  105. //    A 'Mcmd" resource with the same ID as the 'MENU' resource contains a
  106. //    list of command numbers. The format of a 'Mcmd' resource is:
  107. //        Number of Commands: n        (2 bytes)
  108. //        Command Num for Item 1        (4 bytes)
  109. //           ...
  110. //        Command Num for Item n        (4 bytes)
  111.  
  112. void    CMessPopup::ReadCommandNumbers(void) {
  113.     Int16        **theMcmdH;
  114.     
  115.     theMcmdH = (Int16 **)::GetResource('Mcmd', GetPopupMenuResID());
  116.     if(theMcmdH != nil){
  117.         mNumCommands = (*theMcmdH)[0];
  118.         if(mNumCommands > 0){
  119.             // Our command numbers list is the same as the 'Mcmd'
  120.             // resource without the 2-byte count at the top. So we
  121.             // can reuse the 'Mcmd' resource Handle by detaching it,
  122.             // shifting the command numbers down by 2-bytes, and
  123.             // resizing it.
  124.         
  125.             ::DetachResource((Handle)theMcmdH);
  126.             mCommandNums = (Int32**)theMcmdH;
  127.             Int32    commandsSize = mNumCommands * sizeof(Int32);
  128.             ::BlockMoveData(*theMcmdH+1, *mCommandNums, commandsSize);
  129.             ::SetHandleSize((Handle) mCommandNums, commandsSize);
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135. // ---------------------------------------------------------------------------
  136. // CommandFromIndex
  137. // ---------------------------------------------------------------------------
  138. // Return the command number for a particular Menu item
  139.  
  140. CommandT    CMessPopup::CommandFromIndex(Int16 inIndex) const {
  141.     Int32        theCommand = 0;
  142.     if(inIndex <= mNumCommands) theCommand = (*mCommandNums)[inIndex-1];
  143.     return theCommand;
  144. }
  145.